package com.ithuang.quartz.service.impl;

import com.ithuang.quartz.domain.SysJob;
import com.ithuang.quartz.service.SysJobService;
import com.ithuang.quartz.utils.ScheduleUtils;
import org.quartz.JobKey;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

import static com.ithuang.quartz.constant.ScheduleConstants.MISFIRE_IGNORE_MISFIRES;

/**
 * 定时任务调度服务
 * @author HTT
 */
@Service
public class SysJobServiceImpl implements SysJobService {

    @Resource
    private Scheduler scheduler;

    /**
     * 模拟从数据库获取数据，这边偷懒了
     * @return
     */
    public List<SysJob> initTaskList(){
        List<SysJob> list = new ArrayList<>();
        SysJob job = new SysJob();
        job.setJobId(UUID.randomUUID().toString());
        job.setJobGroup("system");
        job.setConcurrent("1");
        job.setCronExpression("0/5 * * * * ?");
        job.setBeanTarget("task1");
        job.setBeanMethodTarget("handle");
        list.add(job);
        job = new SysJob();
        job.setJobId(UUID.randomUUID().toString());
        job.setJobGroup("system");
        job.setConcurrent("1");
        job.setCronExpression("0/50 * * * * ?");
        job.setBeanTarget("task2");
        job.setBeanMethodTarget("handle");
        job.setMisfirePolicy(MISFIRE_IGNORE_MISFIRES);
        list.add(job);
        return list;
    }

    /**
     * 初始化定时任务
     * @throws SchedulerException
     * @throws NoSuchMethodException
     * @throws InvocationTargetException
     * @throws IllegalAccessException
     */
    @PostConstruct
    @Override
    public void init() throws SchedulerException, NoSuchMethodException, InvocationTargetException, IllegalAccessException {
        scheduler.clear();
        List<SysJob> list = initTaskList();
        for (int i = 0 ; i < list.size() ; i ++){
            SysJob job = list.get(i);
            ScheduleUtils.createScheduleJob(scheduler, job);
        }
    }

    @Override
    public int insertJob(SysJob job) throws SchedulerException, InvocationTargetException, NoSuchMethodException, IllegalAccessException {
        ScheduleUtils.createScheduleJob(scheduler, job);
        return 1;
    }

    @Override
    public void run(SysJob job) throws SchedulerException {
        scheduler.triggerJob(JobKey.jobKey(job.getJobId(),job.getJobGroup()));
    }

    @Override
    public int updateJob(SysJob job) throws SchedulerException, InvocationTargetException, NoSuchMethodException, IllegalAccessException {
        // 判断是否存在
        JobKey jobKey = JobKey.jobKey(job.getJobId(),job.getJobGroup());
        if (scheduler.checkExists(jobKey)) {
            scheduler.deleteJob(jobKey);
        }
        ScheduleUtils.createScheduleJob(scheduler, job);
        return 1;
    }

    @Override
    public int pauseJob(SysJob job) throws SchedulerException {
        scheduler.pauseJob(JobKey.jobKey(job.getJobId(),job.getJobGroup()));
        return 1;
    }

    @Override
    public int resumeJob(SysJob job) throws SchedulerException {
        scheduler.resumeJob(JobKey.jobKey(job.getJobId(),job.getJobGroup()));
        return 1;
    }

    @Override
    public int deleteJob(SysJob job) throws SchedulerException {
        scheduler.deleteJob(JobKey.jobKey(job.getJobId(),job.getJobGroup()));
        return 1;
    }





}
